Rain has pummeled the cows' field, a rectangular grid
of R rows and C columns (1 ≤ R ≤ 50, 1 ≤ C ≤ 50). While
good for the grass, the rain makes some patches of bare earth quite muddy. The
cows, being meticulous grazers, don't want to get their hooves dirty while they
eat.
To prevent those muddy hooves, Farmer John will place
a number of wooden boards over the muddy parts of the cows' field. Each of the
boards is 1 unit wide, and can be any length long. Each board must be aligned
parallel to one of the sides of the field.
Farmer John wishes to minimize the number of boards
needed to cover the muddy spots, some of which might require more than one
board to cover. The boards may not cover any grass and deprive the cows of
grazing area but they can overlap each other.
Compute the minimum number of boards FJ requires to
cover all the mud in the field.
Input. First line contains two space-separated
integers: R and C. Each line from 2 to
R+1 contains a
string of C characters, with '*' representing a muddy patch, and '.'
representing a grassy patch. No spaces are present.
Output. Print a single integer
representing the number of boards FJ needs.
Sample Input
4 4
*.*.
.***
***.
..*.
Sample
Output
4
ãðàôû – ìàêñèìàëüíîå ïàðîñî÷åòàíèå
Àíàëèç
àëãîðèòìà
ñì. e-olymp.com 6581
(Àòàêóþùèå ëàäüè). Òî÷êè – ïåøêè, çâåçäî÷êè – ìåñòà, ãäå ìîæíî ðàññòàâëÿòü
ëàäüè.
Ðåàëèçàöèÿ àëãîðèòìà
#include <cstdio>
#include <vector>
#define MAX 1010
using namespace
std;
int i, j, r, c, hor, ver, flow;
char s[MAX][MAX];
int h[MAX][MAX], v[MAX][MAX];
vector<int> used, mt;
vector<int> g[60*60];
int dfs(int
v)
{
if (used[v]) return
0;
used[v] = 1;
for (int i = 0; i
< g[v].size(); i++)
{
int to = g[v][i];
if (mt[to]
== -1 || dfs(mt[to]))
{
mt[to] = v;
return 1;
}
}
return 0;
}
void AugmentingPath(void)
{
int i, j, to;
mt.assign (ver, -1);
for (i = 1; i < hor; i++)
{
used.assign(hor,
0);
dfs(i);
}
}
int main(void)
{
scanf("%d %d\n", &r,&c);
for(i = 1; i <= r; i++)
gets(s[i] + 1);
hor = ver = 1;
for(i = 1; i <= r; ++i)
for(j = 1; j <= c; ++j)
if(s[i][j] == '*')
{
h[i][j] = (s[i][j
- 1] == '*' ? h[i][j - 1] : hor++);
v[i][j] = (s[i -
1][j] == '*' ? v[i - 1][j] : ver++);
g[h[i][j]].push_back(v[i][j]);
}
AugmentingPath();
for (flow = 0, i = 1; i < ver; i++)
if (mt[i] != -1) flow++;
printf("%d\n",flow);
return 0;
}